package com.custom.base.dialog

import android.text.Selection
import android.text.TextUtils
import android.view.View
import android.widget.EditText
import com.custom.base.R
import com.custom.base.util.SDViewUtil.dp2pxInt
import com.custom.base.util.SDViewUtil.hideInputMethod
import com.custom.base.util.SDViewUtil.inflate
import com.custom.base.util.SDViewUtil.layoutParamsLinearLayoutMW
import com.custom.base.util.SDViewUtil.showInputMethod

/**
 * 作者：
 * 时间：2021/1/14
 * 描述：带标题，输入框，确定按钮和取消按钮的窗口
 */
class SDDialogEdit : SDDialogCustom() {
    private lateinit var mEtContent: EditText
    private var mListener: YYKDialogInputListener? = null

    fun setmListener(mListener: YYKDialogInputListener): SDDialogEdit {
        this.mListener = mListener
        return this
    }
    init {
        setmListener(object : SDDialogCustomListener {
            override fun onDismiss(dialog: SDDialogCustom) {
                mListener?.onDismiss(this@SDDialogEdit)
            }

            override fun onClickConfirm(v: View, dialog: SDDialogCustom) {
                mListener?.onClickConfirm(v, mEtContent, this@SDDialogEdit)
            }

            override fun onClickCancel(v: View, dialog: SDDialogCustom) {
                mListener?.onClickCancel(v, this@SDDialogEdit)
            }
        })
        addEdittext()
    }
    fun setTextContent(text: String?): SDDialogEdit {
        if (TextUtils.isEmpty(text)) {
            mEtContent.setText("")
        } else {
            mEtContent.setText(text)
        }
        return this
    }

    fun setTextContent(text: String?, isLast: Boolean): SDDialogEdit {
        if (TextUtils.isEmpty(text)) {
            mEtContent.setText("")
        } else {
            mEtContent.setText(text)
        }
        if (isLast) {
            Selection.setSelection(mEtContent.text, mEtContent.text.length)
        }
        return this
    }

    /**
     * 设置输入框上的提示信息
     *
     * @param prompt
     */
    fun setPrompt(prompt: String?) {
        setPromptC(prompt)
    }

    /**
     * 设置输入框中的提示
     *
     * @param hint
     */
    fun setContentHint(hint: String?) {
        mEtContent.hint = hint
    }

    private fun addEdittext() {
        val view = inflate(R.layout.dialog_input, null)
        mEtContent = view.findViewById<View>(R.id.dialog_input_et_content) as EditText
        mDrawableManager?.apply { view.setBackgroundDrawable(getLayerWhiteStrokeItemSingle(true)) }
        val params = layoutParamsLinearLayoutMW
        val margin = dp2pxInt(10)
        params.bottomMargin = margin
        params.leftMargin = margin
        params.rightMargin = margin
        params.topMargin = margin
        setCustomView(view, params)
    }

    override fun show() {
        super.show()
        showInputMethod(mEtContent,  delay = 200)
    }

    override fun dismiss() {
        hideInputMethod(mEtContent)
        super.dismiss()
    }

    interface YYKDialogInputListener {
        fun onClickCancel(v: View, dialog: SDDialogEdit)
        fun onClickConfirm(
            v: View,
            content: EditText,
            dialog: SDDialogEdit
        )

        fun onDismiss(dialog: SDDialogEdit)
    }
}